package cn.com.xinjinfu.pdoulcProduct.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.AllIncomBean;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;
import cn.com.xinjinfu.pdoulcProduct.view.CircleProgressBar;

/**
 * 累计收益
 * Created by android-adt on 2016/3/10.
 */
public class AllIncomActivity extends BaseActivity {
    private int time = 1500;
    private CircleProgressBar circleBar, circleBar1, circleBar2;
    private RequestQueue requen;
    private TextView zong_income,term_income,demand_incom,reward_income,
            reward_par,term_par,demand_par;
    private AllIncomBean allIncomBean;
    private Dialog dg;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_all_income);
        TitleManager.showTitleWhite(this, "累计收益", "");
        circleBar = (CircleProgressBar) findViewById(R.id.circle);
        circleBar1 = (CircleProgressBar) findViewById(R.id.circle1);
        circleBar2 = (CircleProgressBar) findViewById(R.id.circle2);
        circleBar.setMaxstepnumber(10000);      //设置一圈的最大值
        circleBar.setColor(110, 211, 214);
        circleBar1.setColor(247, 127, 150);
        circleBar2.setColor(244, 185, 66);

        reward_par = (TextView) findViewById(R.id.reward_par);
        term_par = (TextView) findViewById(R.id.term_par);
        demand_par = (TextView) findViewById(R.id.demand_par);

        zong_income = (TextView) findViewById(R.id.zong_income);
        term_income = (TextView) findViewById(R.id.term_income);
        demand_incom = (TextView) findViewById(R.id.demand_income);
        reward_income = (TextView) findViewById(R.id.reward_income);
        requen = Volley.newRequestQueue(this);
        loginRequest();
        if (!DataHandler.isNetworkConnected(this)) {
            if (SharedPreferencesUtils.getUserInfo(this, "allIncomBean") != "") {
                allIncomBean = JSON.parseObject(SharedPreferencesUtils.getUserInfo(this, "allIncomBean"), AllIncomBean.class);
                if (allIncomBean != null) {
                    upData();
                }
            }
        }
    }

    public void upData() {
        if (allIncomBean != null) {
            reward_par.setText(TextViewUtil.getSumLength(allIncomBean.getRewardPercent())+"%");
            term_par.setText(TextViewUtil.getSumLength(allIncomBean.getInvestPercent())+"%");
            demand_par.setText(TextViewUtil.getSumLength(allIncomBean.getExperPercent())+"%");
            zong_income.setText(TextViewUtil.getSumLength(allIncomBean.getTotalIncome()));
            term_income.setText(TextViewUtil.getSumLength(allIncomBean.getExperIncome()));
            demand_incom.setText(TextViewUtil.getSumLength(allIncomBean.getInvestIncome()));
            reward_income.setText(TextViewUtil.getSumLength(allIncomBean.getRewardIncome()));
            circleBar1.update(percent(allIncomBean.getExperPercent())*100, time);
            circleBar.update(percent(allIncomBean.getInvestPercent())*100, time);
            circleBar2.update(percent(allIncomBean.getRewardPercent())*100, time);
        }
    }

    private int percent(double percent){
        if (percent>0&&percent<2){
            return 2;
        }else if (percent>90&&percent<100){
            return 90;
        }
        return (int)percent;
    }
    /**
     * 网络请求
     */
    private void loginRequest() {
        dg = UIManager.getLoadingDialog(this);
        dg.setCanceledOnTouchOutside(false);
        dg.show();
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1008");
            parameters.put("id", user.getCurrUser().getSign());
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
            requen.add(request);
        }
    }

    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dg.dismiss();
            DataHandler.errorHandler(AllIncomActivity.this, error);
        }
    };

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            dg.dismiss();
            L.d("所有收益 ==========" + jobj);
            SharedPreferencesUtils.saveUserInfo(AllIncomActivity.this, "allIncomBean", jobj.toString());
            allIncomBean = JSON.parseObject(jobj.toString(), AllIncomBean.class);
            upData();
        }
    };
}
